﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Database;
using Temp.Model;

namespace Temp.Controller
{
    class PlaceController
    {
        private SQLiteFactory sqlf;
        private List<Temp.Model.Place> placeList;
        private Place place; 
        private SQLite.SQLiteConnection conn;
        
        public PlaceController() {
            sqlf = new SQLiteFactory();
            place = new Place();
        }

        public Place getPlace(int idPlace) {
            conn = sqlf.getConnection();
            place = (conn.Table<Place>().Where(p => p.id == idPlace)).Single();
            conn.Close();
            return place;
        }
        public List<Place> getPlaces()
        {
            List<Place> places = new List<Place>();
            conn = sqlf.getConnection();
            places = conn.Query<Place>("Select * from Place").ToList();
            conn.Close();
            return places;
        }
        /// <summary>
        /// returns the string place from id
        /// </summary>
        /// <param name="Place"></param>
        /// <returns></returns>
        public String getPlaceById(int id)
        {
            conn = sqlf.getConnection();
            
            String str = conn.Query<Place>("Select name from place where id =" + id).Single().name;
            conn.Close();
            return str;
        }
        /// <summary>
        /// returns the string place from id
        /// </summary>
        /// <param name="Place"></param>
        /// <returns></returns>
        public int getPlaceIdByString(String str)
        {
            conn = sqlf.getConnection();

            int x= conn.Query<Place>("Select * from Place where name ='" + str+"'").Single().id;

            conn.Close();
            return x;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="idPlace"></param>
        /// <param name="dir">direction -> north, west, south, east</param>
        /// <returns></returns>
        public int getNextPlace(int idPlace, String dir)
        {
            int newPlace;
            conn = sqlf.getConnection();
            place = (conn.Table<Place>().Where(p => p.id == idPlace)).Single();

            if (dir.Equals("north"))
                newPlace = place.north;

            else if (dir.Equals("south"))
                newPlace = place.south;

            else if (dir.Equals("east"))
                newPlace = place.east;

            else
                newPlace = place.west;

             conn.Close();

            return newPlace;
        } 
       
    }
}
